home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 22 / PCPP #22.iso / Quake2 / q2source_12_11 / utils3 / texpaint / win_cam.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-12  |  8.6 KB  |  393 lines

  1. #include "texpaint.h"
  2.  
  3. #define    CAMERA_WINDOW_CLASS    "TPCamera"
  4.  
  5. HDC        camdc;
  6. HGLRC    baseRC;
  7.  
  8. float    pitch, yaw, roll;
  9. qboolean    model_lines = false;
  10.  
  11. float    cam_x, cam_y=-64, cam_z=32;
  12.  
  13. int        cam_width, cam_height;
  14.  
  15. BINDTEXFUNCPTR BindTextureEXT;
  16.  
  17. void InitIndexTexture (void)
  18. {
  19.     int        i;
  20.  
  21.     BindTextureEXT (GL_TEXTURE_2D, TEXTURE_INDEX);
  22.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  23.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  24.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  25.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  26.  
  27.     for (i=0 ; i<sizeof(index_texture)/4 ; i++)
  28.         index_texture[i] = i+1;
  29.  
  30.     glTexImage2D (GL_TEXTURE_2D, 0, 3, width2, height2, 0, GL_RGBA, GL_UNSIGNED_BYTE, index_texture);
  31.  
  32.     BindTextureEXT (GL_TEXTURE_2D, TEXTURE_SKIN);
  33.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  34.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  35.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  36.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  37. }
  38.  
  39. void CreateDisplaylist (void)
  40. {
  41. }
  42.  
  43. void DrawModel (void)
  44. {
  45.     int        i, j;
  46.  
  47.     glColor4f (1,1,1,1);
  48.  
  49.     glBegin (GL_TRIANGLES);
  50.     for (i=0 ; i<numfaces ; i++)
  51.     {
  52.         for (j=0 ; j<3 ; j++)
  53.         {
  54.             glTexCoord2f (tmcoords[i][j][0], tmcoords[i][j][1]);
  55.             glVertex3fv (faces[i].verts[j]);
  56.         }
  57.     }
  58.     glEnd ();
  59. }
  60.  
  61. /*
  62. =============
  63. Cam_Click
  64. =============
  65. */
  66. int        cam_last_index;
  67. void Cam_Click (int x, int y, qboolean shift)
  68. {
  69.     int        index;
  70.     index = 0;
  71.     glReadBuffer (GL_BACK);
  72.     glReadPixels (x, y, 1,1, GL_RGB, GL_UNSIGNED_BYTE, &index);
  73.  
  74.     index--;
  75.     if (index == -1)
  76.         return;
  77.     if (index >= width2*height2)
  78.         return;
  79.  
  80.     if (index == cam_last_index)
  81.         return;        // in same pixel
  82.     cam_last_index = index;
  83.     if (shift)
  84.     {
  85.         Pal_SetIndex (pic[index]);
  86.         return;
  87.     }
  88.  
  89.     SetSkin (index, selected_rgb);
  90.     UpdateWindow (camerawindow);
  91. }
  92.  
  93.  
  94. void Cam_DrawSetup (void)
  95. {
  96.     glViewport (0,0,cam_width, cam_height);
  97.     glMatrixMode (GL_PROJECTION);
  98.     glLoadIdentity ();
  99.     gluPerspective (90,  (float)cam_width/cam_height,  2,  1024);
  100.     gluLookAt (cam_x, cam_y, cam_z,   cam_x, cam_y+1, cam_z,  0, 0, 1);
  101.  
  102.     glRotated (-roll*0.3, 0, 1, 0);
  103.     glRotated (-pitch*0.3, 1, 0, 0);
  104.     glRotated (yaw*0.3, 0, 0, 1);
  105.  
  106.     glMatrixMode (GL_MODELVIEW);
  107.     glLoadIdentity ();
  108.  
  109.     glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  110.     glEnable (GL_DEPTH_TEST);
  111.     glEnable (GL_CULL_FACE);
  112.     glEnable (GL_TEXTURE_2D);
  113.     glCullFace (GL_FRONT);
  114.     
  115.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  116. }
  117.  
  118. void Cam_Draw (void)
  119. {
  120.     if (!cam_width || !cam_height)
  121.         return;
  122.  
  123.     glClearColor (0.3,0.3,0.3,1);
  124.     Cam_DrawSetup ();
  125.  
  126.     BindTextureEXT (GL_TEXTURE_2D, TEXTURE_SKIN);
  127.  
  128.     DrawModel ();
  129.  
  130.     if (model_lines)
  131.     {
  132.         glDisable (GL_TEXTURE_2D);
  133.         glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
  134.         glDepthFunc (GL_LEQUAL);
  135.         glDepthRange (0, 0.999);    // nudge depth to avoid dropouts
  136.         DrawModel ();
  137.         glDepthRange (0, 1);
  138.  
  139.         glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
  140.         glEnable (GL_TEXTURE_2D);
  141.     }
  142.  
  143.     SwapBuffers(camdc);
  144.  
  145.     // now fill the back buffer with the index texture
  146.     glClearColor (0,0,0,0);
  147.     glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  148.     BindTextureEXT (GL_TEXTURE_2D, TEXTURE_INDEX);
  149.     DrawModel ();
  150.  
  151.     BindTextureEXT (GL_TEXTURE_2D, TEXTURE_SKIN);
  152. }
  153.  
  154.  
  155.  
  156. /*
  157. ============
  158. CameraWndProc
  159. ============
  160. */
  161. LONG WINAPI WCam_WndProc (
  162.     HWND    hWnd,
  163.     UINT    uMsg,
  164.     WPARAM  wParam,
  165.     LPARAM  lParam)
  166. {
  167.     LONG    lRet = 1;
  168.     int        fwKeys, xPos, yPos;
  169.     RECT    rect;
  170.     static int    oldx, oldy;
  171.     POINT    pt;
  172.  
  173.     GetClientRect(hWnd, &rect);
  174.     cam_width = rect.right-rect.left;
  175.     cam_height = rect.bottom-rect.top;
  176.  
  177.     switch (uMsg)
  178.     {
  179.     case WM_CREATE:
  180.         camdc = GetDC(hWnd);
  181.         bSetupPixelFormat(camdc);
  182.  
  183.         baseRC = wglCreateContext( camdc );
  184.         if (!baseRC)
  185.             Sys_Error ("wglCreateContext failed");
  186.         if (!wglMakeCurrent( camdc, baseRC ))
  187.             Sys_Error ("wglMakeCurrent failed");
  188.         BindTextureEXT = (void *)wglGetProcAddress((LPCSTR) "glBindTextureEXT");
  189.         if (!BindTextureEXT)
  190.             Sys_Error ("GetProcAddress for BindTextureEXT failed");
  191.  
  192.         break;
  193.     case WM_PAINT:
  194.         { 
  195.             PAINTSTRUCT    ps;
  196.  
  197.             BeginPaint(hWnd, &ps);
  198.             if (!wglMakeCurrent( camdc, baseRC ))
  199.                 Sys_Error ("wglMakeCurrent failed");
  200.             Cam_Draw ();
  201.             EndPaint(hWnd, &ps);
  202.         }
  203.         break;
  204.  
  205.         case WM_MBUTTONDOWN:
  206.         case WM_RBUTTONDOWN:
  207.             if (GetTopWindow(mainwindow) != hWnd)
  208.                 BringWindowToTop(hWnd);
  209.  
  210.             SetFocus (camerawindow);
  211.             SetCapture (camerawindow);
  212.             GetCursorPos (&pt);
  213.             xPos = pt.x;
  214.             yPos = pt.y;
  215.             oldx = xPos;
  216.             oldy = yPos;
  217.             break;
  218.  
  219.         case WM_LBUTTONDOWN:
  220.             cam_last_index = -1;
  221. draw:
  222.             if (GetTopWindow(mainwindow) != hWnd)
  223.                 BringWindowToTop(hWnd);
  224.  
  225.             SetFocus (camerawindow);
  226.             SetCapture (camerawindow);
  227.             fwKeys = wParam;        // key flags 
  228.             xPos = (short)LOWORD(lParam);  // horizontal position of cursor 
  229.             yPos = (short)HIWORD(lParam);  // vertical position of cursor 
  230.             yPos = (int)rect.bottom - 1 - yPos;
  231.             if (!wglMakeCurrent( camdc, baseRC ))
  232.                 Sys_Error ("wglMakeCurrent failed");
  233.  
  234.             Cam_Click (xPos, yPos, !!(wParam&(MK_SHIFT|MK_CONTROL)) );
  235.  
  236. //            Cam_MouseDown (xPos, yPos, fwKeys);
  237.             break;
  238.  
  239.         case WM_MBUTTONUP:
  240.         case WM_RBUTTONUP:
  241.         case WM_LBUTTONUP:
  242.             if (! (wParam & (MK_LBUTTON|MK_RBUTTON|MK_MBUTTON)))
  243.                 ReleaseCapture ();
  244.             break;
  245.  
  246.         case WM_MOUSEMOVE:
  247.             {
  248.                 int        dx, dy;
  249.  
  250.                 if (wParam & MK_LBUTTON)
  251.                     goto draw;
  252.  
  253.                 GetCursorPos (&pt);
  254.                 xPos = pt.x;
  255.                 yPos = pt.y;
  256.                 if (!(wParam & (MK_RBUTTON|MK_MBUTTON)))
  257.                 {
  258.                     oldx = xPos;
  259.                     oldy = yPos;
  260.                     break;
  261.                 }
  262.                 dx = xPos-oldx;
  263.                 dy = oldy-yPos;
  264.                 if (!dx && !dy)
  265.                     break;
  266.                 SetCursorPos (oldx, oldy);
  267.  
  268.                 if (wParam == (MK_RBUTTON|MK_CONTROL) )
  269.                 {
  270.                     if (abs(dx) > abs(dy))
  271.                         cam_y -= 0.1*dx;
  272.                     else
  273.                         cam_y -= 0.1*dy;
  274.                     InvalidateRect (camerawindow, NULL, false);
  275.                 }
  276.                 if (wParam == MK_RBUTTON)
  277.                 {
  278.                     cam_x -= 0.1*dx;
  279.                     cam_z -= 0.1*dy;
  280.                     InvalidateRect (camerawindow, NULL, false);
  281.                 }
  282.                 if (wParam == (MK_MBUTTON|MK_CONTROL) )
  283.                 {
  284.                     if (abs(dx) > abs(dy))
  285.                         roll -= dx;
  286.                     else
  287.                         roll -= dy;
  288.                     InvalidateRect (camerawindow, NULL, false);
  289.                 }
  290.                 if (wParam == MK_MBUTTON)
  291.                 {
  292.                     yaw += dx;
  293.                     pitch += dy;
  294.                     InvalidateRect (camerawindow, NULL, false);
  295.                 }
  296.             }
  297.             break;
  298.  
  299.  
  300.  
  301.         case WM_SIZE:
  302. //            camera.width = rect.right;
  303. //            camera.height = rect.bottom;
  304.             InvalidateRect(camerawindow, NULL, false);
  305.             break;
  306.         case WM_NCCALCSIZE:// don't let windows copy pixels
  307.             lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
  308.             return WVR_REDRAW;
  309.            case WM_CLOSE:
  310.             /* call destroy window to cleanup and go away */
  311.             DestroyWindow (hWnd);
  312.         break;
  313.  
  314.            case WM_DESTROY:
  315.         {
  316.             HGLRC hRC;
  317.             HDC      hDC;
  318.  
  319.                 /* release and free the device context and rendering context */
  320.             hRC = wglGetCurrentContext();
  321.             hDC = wglGetCurrentDC();
  322.  
  323.             wglMakeCurrent(NULL, NULL);
  324.  
  325.             if (hRC)
  326.                 wglDeleteContext(hRC);
  327.             if (hDC)
  328.                 ReleaseDC(hWnd, hDC);
  329.         }
  330.         break;
  331.  
  332.         default:
  333.             /* pass all unhandled messages to DefWindowProc */
  334.             lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
  335.         break;
  336.     }
  337.  
  338.     /* return 1 if handled message, 0 if not */
  339.     return lRet;
  340. }
  341.  
  342.  
  343. /*
  344. ==============
  345. WCam_Register
  346. ==============
  347. */
  348. void WCam_Register (HINSTANCE hInstance)
  349. {
  350.     WNDCLASS   wc;
  351.  
  352.     /* Register the camera class */
  353.     memset (&wc, 0, sizeof(wc));
  354.  
  355.     wc.style         = 0;
  356.     wc.lpfnWndProc   = (WNDPROC)WCam_WndProc;
  357.     wc.cbClsExtra    = 0;
  358.     wc.cbWndExtra    = 0;
  359.     wc.hInstance     = hInstance;
  360.     wc.hIcon         = 0;
  361.     wc.hCursor       = LoadCursor (NULL,IDC_ARROW);
  362.     wc.hbrBackground = NULL;
  363.     wc.lpszMenuName  = 0;
  364.     wc.lpszClassName = CAMERA_WINDOW_CLASS;
  365.  
  366.     if (!RegisterClass (&wc) )
  367.         Sys_Error ("WCam_Register: failed");
  368. }
  369.  
  370.  
  371. void WCam_Create (HINSTANCE hInstance)
  372. {
  373.     WCam_Register (hInstance);
  374.  
  375.     camerawindow = CreateWindow (CAMERA_WINDOW_CLASS ,
  376.         "Camera View",
  377.         QE3_STYLE,
  378.         0,
  379.         0,
  380.         (int)(screen_width*0.5),
  381.         (int)(screen_height-20),    // size
  382.  
  383.         mainwindow,    // parent window
  384.         0,        // no menu
  385.         hInstance,
  386.         0);
  387.     if (!camerawindow)
  388.         Sys_Error ("Couldn't create camerawindow");
  389.  
  390.     RestoreWindowState(camerawindow, "camerawindow");
  391.     ShowWindow (camerawindow, SW_SHOWDEFAULT);
  392. }
  393.